home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Utilities / Winter Shell 1.0d2 / Source / Libraries / EventLib / EventDebugLib.c next >
Encoding:
C/C++ Source or Header  |  1993-11-30  |  4.3 KB  |  186 lines  |  [TEXT/KAHL]

  1. #ifdef DEBUG_EVENT
  2.  
  3. #include <stdio.h>
  4. #include "EventPrivateLib.h"
  5.  
  6. static Boolean debug_print_long_event_list = false;
  7. static Boolean debug_print_short_event_list = false;
  8. static Boolean debug_print_event = false;
  9. static Boolean debug_print_object = false;
  10.  
  11. static const char *EventKindName(EventKindType kind)
  12. {
  13.     int i;
  14.     static struct {
  15.         EventKindType kind;
  16.         char *name;
  17.     } names[EVENT_LAST+1] = {
  18.         { EVENT_NONE,        "NONE", },
  19.         { EVENT_WINDOW,    "WINDOW", },
  20.         { EVENT_DIALOG,    "DIALOG", },
  21.         { EVENT_MODAL,        "MODAL", },
  22.         { EVENT_CONTROL,    "CONTROL", },
  23.         { EVENT_CHECKBOX,    "CHECKBOX", },
  24.         { EVENT_RADIO,        "RADIO", },
  25.         { EVENT_BUTTON,    "BUTTON", },
  26.         { EVENT_SBAR,        "SBAR", },
  27.         { EVENT_TXSCRL,    "TXSCRL", },
  28.         { EVENT_TXDOC,        "TXDOC", },
  29.         { EVENT_TXWIN,        "TXWIN", },
  30.         { EVENT_LIST,        "LIST", },
  31.         { EVENT_CLIPBOARD,"CLIPBOARD", },
  32.         { EVENT_PROGRESS,    "PROGRESS", },
  33.         { EVENT_HELP,        "HELP", },
  34.         { EVENT_ABOUT,        "ABOUT", },
  35.         { EVENT_WINMENU,    "WINMENU", },
  36.         { EVENT_DEBUG,        "DEBUG", },
  37.         { EVENT_APPLICATION,    "APPLICATION", },
  38.     };
  39.     for (i = 0; i < EVENT_LAST && kind != names[i].kind; i++)
  40.         ;
  41.     return((kind == names[i].kind) ? names[i].name : "UNKNOWN");
  42. }
  43.  
  44. #include <stdarg.h>
  45.  
  46. #ifdef TOO_SLOW
  47.  
  48. #include "ResourceConstantsLib.h"
  49. #include "TextLib.h"
  50. #include "TextScrollLib.h"
  51. #include "WindowMenuLib.h"
  52.  
  53. /* display debug text in a scrollable window */
  54. static void DebugPrint(const char *fmt, ...)
  55. {
  56.     static WindowPtr dbgwin;
  57.     static TextScrollHandle scrl;
  58.     static Boolean printing;
  59.     TextHandle text = NULL;
  60.     Rect bounds;
  61.     va_list ap;
  62.     CStr255 s;
  63.     char *p;
  64.     TextStyle style;
  65.     
  66.     TRY {
  67.         if (! printing) {
  68.             printing = true;
  69.             if (! dbgwin) {
  70.                 TRY {
  71.                     dbgwin = WinGet(RLW_TEXT);
  72.                     WinPortRect(win, &bounds);
  73.                     scrl = TxScrlBegin(dbgwin, &bounds, true, true);
  74.                     style.tsSize = 9;
  75.                     TxScrlSetStyle(scrl, doSize, &style);
  76.                     TxScrlWrap(scrl, false);
  77.                     WinTitleSet(dbgwin, "Debug");
  78.                     WinMenuAdd(dbgwin);
  79.                     WinShow(dbgwin);
  80.                 } CATCH {
  81.                     TxScrlEnd(scrl); scrl = NULL;
  82.                     WinEnd(dbgwin); dbgwin = NULL;
  83.                 } ENDTRY;
  84.             }
  85.             va_start(ap, fmt);
  86.             vsprintf(s, fmt, ap);
  87.             va_end(ap);
  88.             for (p = s; *p; p++) {
  89.                 if (*p == '\n')
  90.                     *p = '\r';
  91.                 else if (*p == '\t')
  92.                     *p = ' ';
  93.             }
  94.             text = TxScrlText(scrl);
  95.             TxSelect(text, TxLength(text), TxLength(text));
  96.             TxInsert(text, s, strlen(s));
  97.             TxScrlChanged(scrl);
  98.             printing = false;
  99.         }
  100.     } CATCH {
  101.         printing = false;
  102.     } ENDTRY;
  103. }
  104.  
  105. #else /* TOO_SLOW */
  106.  
  107. static void DebugPrint(const char *fmt, ...)
  108. {
  109.     va_list ap;
  110.  
  111.     va_start(ap, fmt);
  112.     vprintf(fmt, ap);
  113.     va_end(ap);
  114. }
  115.  
  116. #endif /* TOO_SLOW */
  117.  
  118. void EventPrintObject(const char *prompt, EventType *obj,
  119.     const char *flagprompt, Boolean flag)
  120. {
  121.     if (debug_print_object) {
  122.         DebugPrint("%s: object=0x%lx, kind=%s, id=%ld, %s=%s\n",
  123.             prompt, obj->object, EventKindName(obj->kind), obj->id,
  124.             flagprompt, flag ? "true" : "false");
  125.     }
  126. }
  127.  
  128. /* print the focus list */
  129. void EventPrintList(const char *prompt, WindowPtr window, EventType *list)
  130. {
  131.     CStr255 title;
  132.     EventType *item = NULL;
  133.     
  134.     WinTitle(window, title);
  135.     if (debug_print_long_event_list) {
  136.         DebugPrint("%s: window = '%s' (0x%8.8lx)\n", prompt, title, window);
  137.         DebugPrint("  objects = ");
  138.         for (item = list; item; item = item->next)
  139.             DebugPrint("%8.8lx ", item->object);
  140.         DebugPrint("\n");
  141.         DebugPrint("  kinds   = ");
  142.         for (item = list; item; item = item->next)
  143.             DebugPrint("%-8.8s ", EventKindName(item->kind));
  144.         DebugPrint("\n\n");
  145.     }
  146.     else if (debug_print_short_event_list) {
  147.         DebugPrint("%s: window = '%s'; objects = ", prompt, title);
  148.         for (item = list; item; item = item->next)
  149.             DebugPrint("%s ", EventKindName(item->kind));
  150.         DebugPrint("\n");
  151.     }
  152. }
  153.  
  154. /* print the event */
  155. void EventPrint(const char *prompt, EventRecord *event)
  156. {
  157.     #define NEVENT (10)
  158.     struct {
  159.         int what;
  160.         char *name;
  161.     } names[NEVENT] = {
  162.         { nullEvent, "nullEvent", },
  163.         { mouseDown, "mouseDown", },
  164.         { mouseUp, "mouseUp", },
  165.         { keyDown, "keyDown", },
  166.         { keyUp, "keyUp", },
  167.         { autoKey, "autoKey", },
  168.         { updateEvt, "updateEvt", },
  169.         { diskEvt, "diskEvt", },
  170.         { activateEvt, "activateEvt", },
  171.         { osEvt, "osEvt", },
  172.     };
  173.     int i;
  174.     
  175.     if (debug_print_event) {
  176.         for (i = 0; i < NEVENT && event->what != names[i].what; i++)
  177.             ;
  178.         if (i < NEVENT) {
  179.             DebugPrint("%s: event->what = %s; event->message = 0x%.8lx\n",
  180.                 prompt, names[i].name, event->message);
  181.         }
  182.     }
  183. }
  184.  
  185. #endif /* DEBUG_EVENT */
  186.